package com.sh.xxj.rebot.service;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * PNG图片与TTF文字合成系统.
 * 
 * @author Administrator
 * 
 */
public class PngTtfCompositeService {

	public static void main(String[] args) throws IOException {
		// F:\study_machine\test

//		PngTtfCompositeService service = new PngTtfCompositeService();
//		service.pngTtfComposite("F:\\study_machine\\test\\m1_pic_base.png",
//				"F:\\study_machine\\test\\m2.jpg",
//				"F:\\study_machine\\test\\m2c.png", null, null);
		
		PngTtfCompositeService service = new PngTtfCompositeService();
		service.pngTtfComposite("e:\\png_comp\\base.png",
				"e:\\png_comp\\pic1.png",
				"e:\\png_comp\\c1.png", null, null);
	}

	/**
	 * Png和Ttf合成
	 * 
	 * @param cardBasePngPath
	 * @param inputPngPath
	 * @param outputPngPath
	 * @param ttf
	 * @param words
	 * @throws IOException
	 */
	public void pngTtfComposite(String cardBasePngPath, String inputPngPath,
			String outputPngPath, String ttf, String words) throws IOException {
		File inputFile = new File(inputPngPath);
		BufferedImage inputImage = ImageIO.read(inputFile);
		File cardBaseFile = new File(cardBasePngPath);
		BufferedImage cardBaseImage = ImageIO.read(cardBaseFile);
		BufferedImage outputImage = null;
		// 图片缩放
		// AffineTransform transform = AffineTransform.getScaleInstance(0.5,
		// 0.5);// 返回表示缩放变换的变换
		// AffineTransformOp op = new AffineTransformOp(transform,
		// AffineTransformOp.TYPE_BILINEAR);
		// dstImage = op.filter(sourceImage, null);

		//得到画图对象
		Graphics2D graphics2d = cardBaseImage.createGraphics();
		//背景上画卡片
		graphics2d.drawImage(inputImage, null, 15, 15);
		
		//画字体
		Font font=FontService.getInstance().getFont();
		graphics2d.setFont(font);
		graphics2d.setColor(Color.RED);
		graphics2d.drawString("私は知っている", 70, 220);
		
		outputImage = cardBaseImage;

		/********** save到本地 *****************/
		try {
			ImageIO.write(outputImage, "png", new File(outputPngPath));
		} catch (IOException e) {
			e.printStackTrace();
		}
		/********** save end *****************/
	}

}
